package com.god.graph.node;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

public class ImageNodeGenerator implements NodeGenerator<String> {
	public String createNode(Map fieldsMap,String nodeName) {
	    try {
	        int width = 200, height = 200;

	        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
	        // into integer pixels
	        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	        Graphics2D ig2 = bi.createGraphics();
	        Iterator itr = fieldsMap.keySet().iterator();
	        int fontHeight = 10;
	        while(itr.hasNext()) {
	        	Map.Entry pairs = (Map.Entry)itr.next();
	        	String key = (String)pairs.getKey();
	        	Object value = pairs.getValue();

	        	ig2.drawString(key + "-" + value,10 , (10 + fontHeight));
	        }
	        //TODO do rectangle around the text
	        //ig2.drawRect(0, 0, 55,55);
	        
	        ImageIO.write(bi, "PNG", new File(nodeName));
	        
	      } catch (IOException ie) {
	        ie.printStackTrace();
	      }

	      return nodeName;
	}
}
